package com.wwu.system.controller;

import com.wwu.common.constant.CommConst;
import com.wwu.common.web.ResultObj;
import com.wwu.common.web.ResultPage;
import com.wwu.system.entity.OperLog;
import com.wwu.system.service.IOperLogService;
import com.wwu.system.vo.QueryVO;
import io.swagger.annotations.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Arrays;

/**
 * 操作日志监控前端控制器
 * @author 一蓑烟雨
 * @version 1.0.0
 * @date 2023-07-10 10:31
 **/
@Api(tags = "操作日志监控控制器(SysLogController)")
@ApiSort(3)
@Controller
@RequestMapping("/system/log")
public class OperLogController {
    @Resource
    private IOperLogService operLogService;
    
    /**
     * 请求转发到操作日志主页
     * @return java.lang.String
     * @author 一蓑烟雨
     * @date 2023/7/11 10:46
     */
    @GetMapping("/operLogMain")
    public String operLogMainHtml() {
        return "system/log/oper-log-main";
    }

    /**
     * 请求转发到操作日志详情页面
     * @param recordId  操作日志id
     * @param model
     * @return java.lang.String
     * @author 一蓑烟雨
     * @date 2023/7/29 17:51
     */
    @GetMapping("/operLogDetail")
    public String operLogDetailHtml(String recordId, Model model) {
        if(StringUtils.hasText(recordId)){
            model.addAttribute("operLog",operLogService.getById(recordId));
        }
        return "system/log/oper-log-detail";
    }

    @ApiOperation(value = "分页查询操作日志信息")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页数",paramType = "query", required = true),
            @ApiImplicitParam(name = "limit", value = "每页条数",paramType = "query", required = true),
            @ApiImplicitParam(name = "operLog", value = "系统操作日志实体"),
            @ApiImplicitParam(name = "queryVO", value = "前端查询条件封装实体类"),
    })
    @GetMapping("/operLoglist")
    @ResponseBody
    public ResultPage<OperLog> operLoglist(@RequestParam(defaultValue = "1") Integer page,
                                           @RequestParam(defaultValue = "5000") Integer limit,
                                           OperLog operLog, QueryVO queryVO){
        return operLogService.getOperLoglistPage(page,limit,operLog,queryVO);
    }

    @ApiOperation(value = "添加操作日志信息")
    @ApiOperationSupport(order = 2)
    @PostMapping("/saveOperLog")
    @ResponseBody
    public ResultObj saveOperLog(@RequestBody OperLog operLog){
        operLogService.saveOperLog(operLog);
        return ResultObj.success("操作日志信息添加成功");
    }

    @ApiOperation(value = "删除操作日志信息")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParam(name = "recordIds", value = "日志记录id集合，以逗号分隔")
    @PutMapping("/deleteOperLog")
    @ResponseBody
    public ResultObj deleteOperLog(String recordIds){
        if(StringUtils.hasText(recordIds)){
            operLogService.deleteOperLog(Arrays.asList(recordIds.split(CommConst.SPLIT_COMMA)));
        }else{
            operLogService.deleteOperLog(null);
        }
        return ResultObj.success("操作日志信息删除成功");
    }
}
